const { sm2, sm4 } = require('miniprogram-sm-crypto');
const JSSM4 = require('jssm4');
const CryptoJS = require('crypto-js');
const tools = {
    // SM2加密
    encryptSM2(text, key) {
        key =
            key ||
            "04c3fc1b4665ce498fdc670ae9227cde389e86557846579d276d4c2f38eca4f8a916987a612828811565cb250fc767af5f7ebf60a1ef646ac6bd1a3a3077d6e05c"
        const enText = sm2.doEncrypt(text, key, 1)
        return "04" + enText
    },
    // SM2解密
    decryptSM2(text, key) {
        key =
            key || "5eb3be6e8b9a59ef0424be53ee1ca73396a0e619db5d67c035ca2936238fc28f"
        const deText = sm2.doDecrypt(text.slice(2), key, 1)
        return deText
    },
      // sm4加密
      encryptSM4 (text, key) {
        key = key || "11fa0b98b4974c0d"
        let sm4 = new JSSM4(key)
        return sm4.encryptData_ECB(text)
    },
    // sm4解密
    decryptSM4 (text, key) {
        key = key || "11fa0b98b4974c0d"
        let sm4 = new JSSM4(key)
        return sm4.decryptData_ECB(text)
    },
      //AES加密
      encryptAES (word, keyStr) {
        keyStr = keyStr ? keyStr : "ad555e777edc4c34a99d546926221d1f" //判断是否存在ksy，不存在就用定义好的key
        let key = CryptoJS.enc.Utf8.parse(keyStr)
        let srcs = CryptoJS.enc.Utf8.parse(word)
        let encrypted = CryptoJS.AES.encrypt(srcs, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return encrypted.toString()
    },
    //AES解密
    decryptAES (word, keyStr) {
        keyStr = keyStr ? keyStr : "ad555e777edc4c34a99d546926221d1f"
        let key = CryptoJS.enc.Utf8.parse(keyStr)
        let decrypt = CryptoJS.AES.decrypt(word, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return CryptoJS.enc.Utf8.stringify(decrypt).toString()
    },
};
export {
    tools
};